﻿using MathNet.Numerics;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vampirewal.Core;
using Vampirewal.Core.Attributes;
using Vampirewal.Core.SimpleMVVM;
using Vampirewal.Core.WPF.Theme;
//using Vampirewal.Core.VContainer;

//using YC.WPF.Theme;

namespace test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    //[VampirewalIoCRegister("MainView", RegisterType.Window)]
    [VampirewalIoCRegister("MainView")]
    public partial class MainWindow1 : MainWindowBase
    {
        public MainWindow1()
        {
            InitializeComponent();
            //InitData();
            //SizeChanged += MainWindow1_SizeChanged;
            //this.DataContext=VampirewalCoreContext.GetInstance().Services.GetService<MainViewModel2>();

            UseVampirewalNotifyIcon = true;

            CreateConnections();
        }

        public override string ViewModelKey => "MainViewModel2";

        protected override List<MenuItem> SetVampirewalNotifyIconContextMenu()
        {
            var mv2= this.DataSource as MainViewModel2;

            return new List<MenuItem>()
            {

                new MenuItem()
                {
                    Header="测试",
                    Command=mv2.TestCommand
                },
                new MenuItem()
                {
                    Header="退出",
                    Command=CloseWindowCommand
                }
            };
        }

        // 在代码中创建连接线
        private void CreateConnections()
        {
            //// 获取节点
            //var startNode = (Node)flowChart.Children[0];
            //var processNode = (Node)flowChart.Children[1];
            //var decisionNode = (Node)flowChart.Children[2];
            //var endNode = (Node)flowChart.Children[3];

            // 获取连接点
            var startBottom = flowChart.GetConnectionPoint(Start, 2); // 底部连接点
            var processTop = flowChart.GetConnectionPoint(Process, 0); // 顶部连接点

            var processBottom = flowChart.GetConnectionPoint(Process, 2); // 底部连接点
            var decisionLeft = flowChart.GetConnectionPoint(Decision, 3); // 左侧连接点

            var decisionBottom = flowChart.GetConnectionPoint(Decision, 2); // 底部连接点
            var endTop = flowChart.GetConnectionPoint(End, 0); // 顶部连接点

            // 创建连接线
            flowChart.AddConnectionLine(startBottom, processTop);
            flowChart.AddConnectionLine(processBottom, decisionLeft);
            flowChart.AddConnectionLine(decisionBottom, endTop);
        }


        //[VampirewalIoCGetInstance(  "MainViewModel2")]
        //public override ViewModelBase DataSource 
        //{
        //    get
        //    {
        //        return base.DataSource;
        //    }
        //    set
        //    {
        //        base.DataSource = value;
        //        //DataContext = value;
        //    }
        //}






        //private void MainWindow1_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    MyGrid.Children.Clear();
        //    GridTool.Draw(MyGrid);
        //    DrawCircle(MyGrid);
        //    DrawPath(MyGrid);
        //}

        //private readonly Polyline _line = new Polyline();
        ////private readonly Polygon polygon = new Polygon;
        //private readonly PointCollection _collection = new PointCollection();
        //private readonly Random _random = new Random();

        //private void ButtonPath_OnClick(object sender, RoutedEventArgs e)
        //{
        //    _collection.Add(new Point(_random.Next(1, (int)ActualWidth), _random.Next(1, (int)ActualHeight)));
        //    MyGrid.Children.Clear();
        //    GridTool.Draw(MyGrid);
        //    DrawPath(MyGrid);
        //}

        //private void DrawPath(Panel panel)
        //{
        //    _line.Points = _collection;
        //    _line.Stroke = new SolidColorBrush(Colors.Black);
        //    _line.StrokeThickness = 1;
        //    panel.Children.Add(_line);
        //}

        //private void DrawCircle(Panel panel)
        //{
        //    double currentSize = 20d;

        //    var Rectangle = new Rectangle()
        //    {
        //        Width = currentSize,
        //        Height = currentSize,
        //        Fill = Brushes.Red
        //    };


        //    var translateTransform = new TranslateTransform(20, 20);
        //    Rectangle.RenderTransform = translateTransform;

        //    panel.Children.Add(Rectangle);
        //}

        //private void ButtonDrawCircle_OnClick(object sender, RoutedEventArgs e)
        //{
        //    MyGrid.Children.Clear();
        //    //GridTool.DrawCircle(MyGrid);
        //}


        //private void InitData()
        //{
        //    _collection.Add(new Point(30, 30));
        //    _collection.Add(new Point(50, 50));
        //    //_collection.Add(new Point(60, 40));
        //    //_collection.Add(new Point(80, 120));
        //    //_collection.Add(new Point(120, 140));
        //    //_collection.Add(new Point(200, 180));
        //}


    }

    public static class GridTool
    {
        public static void Draw(Canvas canvas)
        {
            var gridBrush = new SolidColorBrush { Color = Colors.White };

            double scaleX = 20;
            double currentPosY = 0;
            currentPosY += scaleX;
            while (currentPosY < canvas.ActualHeight)
            {
                Line line = new Line
                {
                    X1 = 0,
                    Y1 = currentPosY,
                    X2 = canvas.ActualWidth,
                    Y2 = currentPosY,
                    Stroke = gridBrush,
                    StrokeThickness = 0.1
                };
                canvas.Children.Add(line);

                currentPosY += scaleX;
            }

            double scaleY = 20;
            double currentPosX = 0;
            currentPosX += scaleY;
            while (currentPosX < canvas.ActualWidth)
            {
                Line line = new Line
                {
                    X1 = currentPosX,
                    Y1 = 0,
                    X2 = currentPosX,
                    Y2 = canvas.ActualHeight,
                    Stroke = gridBrush,
                    StrokeThickness = 0.1
                };
                canvas.Children.Add(line);

                currentPosX += scaleY;
            }
        }
    }

    [VampirewalIoCRegister(typeof(ITestInterface))]
    public class TestInterface : ITestInterface
    {
        public void aaa()
        {

        }
    }

    /// <summary>
    /// 
    /// </summary>
    [VampirewalIoCRegister(typeof(IDispatcherService))]
    public partial class DispatcherService : IDispatcherService
    {
        public void BeginInvoke(Action act)
        {
            Application.Current.Dispatcher.BeginInvoke(act);
        }
    }
}
